package com.alibaba.cirtus.ots.application.adapter.ability.facade.serviceV2.f2b;


import com.alibaba.cirtus.ots.application.adapter.ability.facade.serviceV2.kafka.KafkaUtils;
import com.alibaba.cirtus.ots.application.adapter.ability.facade.serviceV2.sap.MnSapIf004V2ServiceImpl;
import com.alibaba.cirtus.ots.application.adapter.ability.model.sdo.F2bMessageSDO;
import com.alibaba.cz.base.tool.log.Log;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.lattice2.epoch.middleware.mq.runtime.EpochMessageService;
import com.epoch.app.otsapplicationadaptersdk.contants.OtsApplicationAdapterSdkConstants;
import com.epoch.app.otsapplicationadaptersdk.sap.MnSapIf004SDO;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

@Component("F2bSalesOrderHandler")
public class F2bSalesOrderHandler implements F2bOrderHandler {

    private final Log log = Log.getLogger(F2bSalesOrderHandler.class);
    @Autowired
    private EpochMessageService messageService;

    private final Integer NUMBER_SEGMENT_FIRST_GROUP_FIRST = 1000000001;

    private final Integer NUMBER_SEGMENT_FIRST_GROUP_SECOND = 1999999999;

    private final Integer NUMBER_SEGMENT_SECOND_GROUP_FIRST = 60000000;

    private final Integer NUMBER_SEGMENT_SECOND_GROUP_SECOND = 64999999;


    public static final DateTimeFormatter SHORT_DATETIME_FORMATTER = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");


    /**
     *
     * 根据字段中的号段来区分是否需要发送消息给F2B
     * 字段：outSourceOrderId
     * 号段：1000000001~1999999999 和 0060000000~0064999999 这两个号段为F2B的单据
     * 如果outSourceOrderId中的编码在以上两个号段内，则需要发 kafka消息
     */
    @Override
    public Boolean getF2bOrderFlag(MnSapIf004SDO mnCrmIf004Request) {

        if(Objects.isNull(mnCrmIf004Request) || StringUtils.isEmpty(mnCrmIf004Request.getOutSourceOrderId())){
            return Boolean.FALSE;
        }


        Long orderCode = Long.valueOf(mnCrmIf004Request.getOutSourceOrderId());

        Boolean f2bOrderFlag =  NUMBER_SEGMENT_FIRST_GROUP_FIRST < orderCode ;

        Boolean f2bOrderFlag2 = orderCode < NUMBER_SEGMENT_FIRST_GROUP_SECOND;

        Boolean f2bOrderFlag3 =  NUMBER_SEGMENT_SECOND_GROUP_FIRST < orderCode ;

        Boolean f2bOrderFlag4 = orderCode < NUMBER_SEGMENT_SECOND_GROUP_SECOND;

        //满足号段
        if((f2bOrderFlag && f2bOrderFlag2) || (f2bOrderFlag4 &&f2bOrderFlag3 )){
            return Boolean.TRUE;
        }
        return Boolean.FALSE;
    }


    @Override
    public void sendMessageToF2b(MnSapIf004SDO mnSapIf004SDO) {
        log.info("F2bSalesOrderHandler_sendMessageToF2b_request:{}", JSONObject.toJSONString(mnSapIf004SDO));
        if(Objects.isNull(mnSapIf004SDO) || CollectionUtils.isEmpty(mnSapIf004SDO.getItems())){
            return ;
        }
        //封装Kafka消息
        F2bMessageSDO f2bMessageSDO = new F2bMessageSDO();
        List<F2bMessageSDO.MaterialCertificateDetail> detailList = mnSapIf004SDO.getItems().stream().map(x -> {
            F2bMessageSDO.MaterialCertificateDetail detail = new F2bMessageSDO.MaterialCertificateDetail();
            detail.setDcSignal(x.getDcSignal());
            detail.setSaleUnitQuantity(x.getSaleUnitQuantity());
            detail.setSaleUnit(x.getInventoryCode());
            detail.setFactoryCode(x.getFactoryCode());
            detail.setOutLineId(x.getOutLineId());
            detail.setScItemId(x.getScItemId());
            detail.setInventoryCode(x.getInventoryCode());
            detail.setOutSourceOrderLineId(x.getOutSourceOrderLineId());
            detail.setOutDeliveryOrderLineId(x.getOutDeliveryOrderLineId());
            detail.setSapType(x.getSapType());
            return detail;
        }).collect(Collectors.toList());
        f2bMessageSDO.setPostingTime(mnSapIf004SDO.getPostingTime());
        f2bMessageSDO.setOutDeliveryOrderId(mnSapIf004SDO.getOutDeliveryOrderId());
        f2bMessageSDO.setOutMaterialCertificateId(mnSapIf004SDO.getOutMaterialCertificateId());
        f2bMessageSDO.setOutSourceOrderId(mnSapIf004SDO.getOutSourceOrderId());
        f2bMessageSDO.setDetailList(detailList);
        String dateStr = LocalDateTime.now().format(SHORT_DATETIME_FORMATTER);
        //发送kafka消息
        log.info("f2b.F2bSalesOrderHandler.sendMessageToF2b-sendMessage,topic:{},key:{},message:{}",OtsApplicationAdapterSdkConstants.MN_KAFKA_INVENTORY_TOPIC(),dateStr,JSON.toJSONString(f2bMessageSDO));


        KafkaUtils.send(OtsApplicationAdapterSdkConstants.MN_KAFKA_INVENTORY_TOPIC(),dateStr,JSON.toJSONString(f2bMessageSDO));
        log.info("f2b.F2bSalesOrderHandler.sendMessageToF2b-sendMessage,topic:{},message:{},result_success",OtsApplicationAdapterSdkConstants.MN_KAFKA_INVENTORY_TOPIC(),JSON.toJSONString(f2bMessageSDO));


    }

    public static void main(String[] args) {
        String num1 ="0060000000";
        String num2 ="0064999999";
        String num3 ="0064989999";
        boolean flag = Integer.valueOf(num3) > Integer.valueOf(num1);
        boolean flag2 = Integer.valueOf(num3) <Integer.valueOf(num2);
        System.out.println(flag);
        System.out.println(Integer.valueOf(num3));
        System.out.println(Integer.valueOf(num1));
        System.out.println(Integer.valueOf(num2));
    }

}
